<?php

namespace common\traits;

use Yii;
use yii\helpers\Html;
use ReflectionClass;

/**
 * Description of Constants
 *
 * @author emhome
 */
trait Constants {

    /**
     * 获取当前类的所有Const常量
     * @param string $prefix 常量前缀
     * @return array
     */
    public static function getConstants($prefix = '') {
        $reflectionClass = new ReflectionClass(__CLASS__);
        $constants = $reflectionClass->getConstants();
        if (!$prefix || empty($constants)) {
            return $constants;
        }
        $constPrefix = rtrim(strtoupper($prefix), '_') . '_';
        $arrayReindexed = [];
        foreach ($constants as $key => $value) {
            if (strpos($key, $constPrefix) === 0) {
                $arrayReindexed[$key] = $value;
            }
        }
        return $arrayReindexed;
    }

    /**
     * 获取当前模型常量标签
     * Get current model const name.
     * 
     * @param mixed $prefix 常量前缀
     * @return array
     */
    public static function getAttributeMark($prefix = false) {
        $constPrefix = strtoupper($prefix) . '_';
        $data = self::getConstants($constPrefix);
        return $data;
    }

    /**
     * 将常量名称格式化成语言包使用的标签
     * @param string $label 标签内容
     * @return string
     */
    public static function formatConstLanguage($label = false) {
        if ($label === false) {
            return 'UNKOWN';
        }
        $keywords = [
            'DISABLED',
            'PAYMODE',
        ];
        foreach ($keywords as $prefix) {
            if (strpos($label, $prefix . '_') === 0) {
                return $label;
            }
        }
        return basename(str_replace('\\', '/', __CLASS__)) . '_' . $label;
    }

    /**
     * 应用范围标签
     * Get member portlet panel.
     * @return string
     */
    public static function markLabel($attribute, $value, $format = true, $options = [], $usecolor = false) {
        $consts = self::getAttributeMark($attribute);
        $prekey = array_search($value, $consts);
        if ($format) {
            $label = self::formatConstLanguage($prekey);
            return [
                'text' => Yii::t('label', $label),
                'color' => [
                    'hex' => Yii::t('color/hex', $label),
                    'class' => Yii::t('color/class', $label),
                ]
            ];
        }
        return $prekey;
    }

    /**
     * 获取Const属性的名称值
     * @param string $attribute 属性名称
     * @return string
     */
    public function getConstAttributeValue($attribute) {
        $consts = self::getConstants($attribute);
        $constName = array_search($this->$attribute, $consts);
        return self::formatConstLanguage($constName);
    }

    /**
     * 应用范围标签
     * Get member portlet panel.
     * @return string
     */
    public static function getMarksList($attribute) {
        $consts = self::getAttributeMark($attribute);
        $data = [];
        foreach ($consts as $key => $vls) {
            $data[$vls] = self::formatConstLanguage($key);
        }
        return $data;
    }

    /**
     * 应用范围标签
     * Get member portlet panel.
     * @return string
     */
    public static function getMarksOptionList($attribute, $promote = false) {
        $consts = self::getAttributeMark($attribute);
        $data = [];
        foreach ($consts as $key => $vls) {
            $data[$vls] = Yii::t('label', self::formatConstLanguage($key));
        }
        if ($promote != false) {
            array_unshift($data, $promote);
        }
        return $data;
    }

    /**
     * 应用范围标签
     * Get member portlet panel.
     * @return string
     */
    public function getMarkLabel($attribute) {
        $consts = self::getAttributeMark($attribute);
        $prekey = array_search($this->$attribute, $consts);
        $label = self::formatConstLanguage($prekey);
        $language = [
            'text' => Yii::t('label', $label),
            'color' => Yii::t('color/class', $label),
        ];
        return Html::label($language['text'], null, [
            'class' => 'label label-sm bg-default bg-' . $language['color']
        ]);
    }

}
